<template>
	<view v-if="shenheStatus != 1 || !shenheAliStatus" class="diy-my-page" @touchstart="pageTouchStart"
		@touchmove="pageTouchMove" @touchend="pageTouchEnd">
		<diyGoodsDetail ref="diyGoodsDetail" v-if="goods_id && goods_id > 0" :goods_id="goods_id"
			:activity_new_user_id="activity_new_user_id" :activity_cut_price_id="activity_cut_price_id" :mdId="mdId"
			:videoId="videoId" :mdData="mdData" :pageScrollTop="pageScrollTop" :hideTopTab="hideTopTab"
			:topTabActiveIndex="topTabActiveIndex" :is_pt="is_pt" :is_seckill="is_seckill" :cutOrder="cutOrder"
			:pt_number="pt_number" :date="date" @goodsBlockLocalData="setGoodsBlockLocal"
			@goodsEvaluateLocalData="setGoodsEvaluateLocal" @goodsRichTextLocalData="setGoodsRichTextLocal" :clearTimer="clearChildTimer">
		</diyGoodsDetail>
		<!-- 全局过渡组件 -->
		<transitionPage></transitionPage>
	</view>
	<Shenhe v-else-if="shenheStatus == 1"></Shenhe>
</template>

<script>
	import Shenhe from "@/components/shenhe/shenhe";
	import diyGoodsDetail from "@/goods/diySystemPage/goodsDetail/index.vue";
	import {
		cutPriceOrderByGoodsId
	} from "@/api/marketing/curPrice";
	import { getStoreSwitch } from "@/api/system";

	export default {
		components: {
			diyGoodsDetail,
			Shenhe
		},
		data() {
			return {
				clearChildTimer: false, // 标识是否清除子组件的定时器
				goods_id: "",
				pt_number: 0, //拼团的团id
				routeData: null,
				mdId: "0",
				mdData: {},
				pageScrollTop: 0,
				scrollStart: 0,
				scrollWay: "",
				hideTopTab: true,
				videoId: 0,
				goodsBlockLocal: {
					t: 0,
					h: 0
				}, // 商品内容区域
				goodsEvaluateLocal: {
					t: 0,
					h: 0
				}, // 商品评价区域
				goodsRichTextLocal: {
					t: 0,
					h: 0
				}, // 商品详情区域
				topTabActiveIndex: 0,
				activity_new_user_id: "",
				activity_cut_price_id: "",
				is_pt: false,
				cutOrder: null,
				is_seckill: 0,
				date: ""
			};
		},
		computed: {
			screenWidth() {
				const res = uni.getSystemInfoSync();
				return res.screenWidth;
			}
		},
		onHide() {
			this.clearTimerInChild()
		},
		onUnload() {
			this.clearTimerInChild()
		},

		onLoad(t) {
			uni.setNavigationBarTitle({
				title : ' '
			})
			this.pt_number = t.pt_number || 0;
			// if (t?.activity_new_user_id) {
			//   this.activity_new_user_id = t.activity_new_user_id;
			// }

			if (t?.activity_cut_price_id) {
				this.activity_cut_price_id = t.activity_cut_price_id;
			}

			if(t?.store_id){
        this.$store.commit("setStoreId", t.store_id);
			}

			if (t?.date) {
				this.date = t.date;
			}

			this.is_pt = t.is_pt || false;
			this.is_seckill = t.is_seckill || 0;

			if (t?.id) {
				this.goods_id = t.id;
				this.routeData = t;
				this.videoId = t.video_id;
				let mdLocalData = uni.getStorageSync("storeMdData");
				if (mdLocalData) {
					let mdData = JSON.parse(mdLocalData);
					if (mdData?.id) {
						this.mdId = mdData.id;
						this.getLocalAddress(this.mdId);
					}
				}
			}
			if (t?.md_id) {
				this.mdId = t.md_id;
				this.getLocalAddress(this.mdId, "saveMd");
				uni.setStorageSync("MdDataReload", "true");
			}
			if (t?.scene) {
				let scene = decodeURIComponent(t.scene);
				let params = this.$utils.sceneDecode(scene);
				this.routeData = params;
				if (params.gid) {
					this.goods_id = params.gid;
					this.routeData.id = params.gid;
				}
				if (params.id) {
					this.goods_id = params.id;
					this.routeData.id = params.id;
				}
				if (params.p) {
					this.is_pt = true;
				}
				if (params.m) {
					this.is_seckill = 1;
				}
				if (params.md_id && params.md_id > 0) {
					this.mdId = params.md_id;
					this.getLocalAddress(this.mdId, "saveMd");
					uni.setStorageSync("MdDataReload", "true");
				}

				if(params?.store_id){
					this.$store.commit("setStoreId", params.store_id);
				}
			}

			
			if (!this.$utils.getStorage('token') && this.$store.getters.has_is_one_store) {
				uni.login({
					success: async (r) => {
						const res = await getStoreSwitch({
							code: r.code
						});
						if (res.code) return;
						let { token } = res.data;
				
						if (token) {
							this.$utils.setStorage("token", token);
						}
					}
				});
			}
		},
		onPageScroll(e) {
			this.pageScrollTop = e.scrollTop;
			if (e.scrollTop >= this.screenWidth / 2) {
				this.hideTopTab = false;
			} else {
				this.hideTopTab = true;
			}

			if (
				e.scrollTop >= this.goodsBlockLocal.t &&
				e.scrollTop <= this.goodsBlockLocal.h
			) {
				this.topTabActiveIndex = 0;
			}
			if (
				e.scrollTop >= this.goodsEvaluateLocal.t &&
				e.scrollTop <= this.goodsEvaluateLocal.h
			) {
				this.topTabActiveIndex = 1;
			}
			if (
				e.scrollTop >= this.goodsRichTextLocal.t &&
				e.scrollTop <= this.goodsRichTextLocal.h
			) {
				this.topTabActiveIndex = 2;
			}
		},
		async onShow() {
			this.clearChildTimer = false;
			// 新加更新自提点
			if(uni.getStorageSync("storeMdData")) this.mdData = JSON.parse(uni.getStorageSync("storeMdData"))

			let isUsePreviewImg = uni.getStorageSync("usePreviewImgApi");
			if (isUsePreviewImg == "true") {
				uni.removeStorageSync("usePreviewImgApi");
				return;
			}

			// 重新渲染组件
			// this.goods_id = '-1';
			// setTimeout(() => {
			//   if (this.routeData) this.goods_id = this.routeData.id;
			// }, 0);

			// 判断是否登录
			const isLogin = this.$utils.isLogin(false);
			if (!isLogin) {
				this.$store.commit(
					"setNoLoginTabarPath",
					`/goods/goods/goods?id=${this.routeData.id}`
				);
				this.$store.commit("setRouteType", "redirectTo");
			} else {
				this.$store.commit("setNoLoginTabarPath", "");
				this.$store.commit("setRouteType", "");

				if (this.activity_cut_price_id) {
					let {
						goods_id,
						activity_cut_price_id: activity_id
					} = this;
					const res = await cutPriceOrderByGoodsId({
						activity_id,
						goods_id
					});
					this.cutOrder = res.cutOrder;
					// this.$nextTick(() => {
					//   if (this.$refs.diyGoodsDetail) {
					//     this.$refs.diyGoodsDetail.getGoodsDetail(this.goods_id);
					//   }
					// });
				}
			}
			// if (this.goods_id) {
			//   this.$nextTick(() => {
			//     if (this.$refs.diyGoodsDetail) {
			//       this.$refs.diyGoodsDetail.getGoodsDetail(this.goods_id);
			//     }
			//   });
			// }
		},
		watch: {},
		methods: {
			
			clearTimerInChild() {
				this.clearChildTimer = true; // 设置清除子组件定时器的标识为 true
			},
			
			async getLocalAddress(id, type) {
				const res = await this.$store.dispatch("getLocation");
				if (res) {
					let {
						longitude,
						latitude
					} = res;
					this.getMdData({
						longitude,
						latitude,
						md_id: id,
						type
					});
				}
			},
			setGoodsBlockLocal(e) {
				if (e?.h) {
					this.goodsBlockLocal.t = e.t;
					this.goodsBlockLocal.h = e.t + e.h;
				}
			},
			setGoodsEvaluateLocal(e) {
				if (e?.h) {
					this.goodsEvaluateLocal.t = e.t;
					this.goodsEvaluateLocal.h = e.t + e.h;
				}
			},
			setGoodsRichTextLocal(e) {
				if (e?.h) {
					this.goodsRichTextLocal.t = e.t;
					this.goodsRichTextLocal.h = e.t + e.h;
				}
			},
			pageTouchStart(e) {
				this.scrollStart = e.touches[0].pageY;
			},
			pageTouchMove(e) {
				let y = e.touches[0].pageY;
				if (y - this.scrollStart > 0) {
					if (this.hideTopTab) return;
					this.hideTopTab = true;
				}
			},
			pageTouchEnd() {
			},
			getMdData(params) {
				this.$allrequest.goods.getMdAddress(params).then((res) => {
					if (!res.code && res.data) {
						this.mdData = res.data;
						if (params.type == "saveMd") {
							uni.setStorageSync("storeMdData", JSON.stringify(res.data));
						}
					}
				});
			},

			getShareInfo() {
				let goodsInfo = this.$refs["diyGoodsDetail"]?.goodsInfo;
				const storeId = uni.getStorageSync("store_id");
				const userInfo = uni.getStorageSync("userInfo");
				const userId = userInfo && userInfo.id > 0 ? userInfo.id : 0;

				// 分享图片路径处理添加https协议
				let imageUrl = goodsInfo.cover_pic;
				if (
					(imageUrl && imageUrl.indexOf("http") == -1) ||
					imageUrl.indexOf("http") > 0
				) {
					imageUrl = "https:" + imageUrl;
				}

				let path =
					`/goods/goods/goods?store_id=${storeId}&id=${this.goods_id}&md_id=${this.mdId}&user_id=${userId}&page_type=1`;
				let title = goodsInfo.name || "";

				if (this.is_pt) {
					path = path + `&is_pt=true`;
					title = "快来一起拼团，" + title;
				}

				if(this.is_seckill){
					path = path + `&is_seckill=${this.is_seckill}`;
				}

				return {
					path,
					title,
					imageUrl
				};
			}
		},
		onShareAppMessage() {
			return this.getShareInfo();
		},
		onShareTimeline() {
			return this.getShareInfo();
		}
	};
</script>

<style lang="scss" scoped>
	.diy-my-page {
		width: 100%;
	}
</style>